package cn.wolfcode.p2p.base.service.impl;

import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.domain.OrderTime;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.domain.VideoAuth;
import cn.wolfcode.p2p.base.exception.DisplayableException;
import cn.wolfcode.p2p.base.mapper.VideoAuthMapper;
import cn.wolfcode.p2p.base.qo.QueryObject;
import cn.wolfcode.p2p.base.service.ILoginInfoService;
import cn.wolfcode.p2p.base.service.IOrderTimeService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.base.service.IVideoAuthService;
import cn.wolfcode.p2p.base.util.BitStateUtil;
import cn.wolfcode.p2p.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service@Transactional
public class VideoAuthServiceImpl implements IVideoAuthService {
    @Autowired
    private VideoAuthMapper videoAuthMapper;
    @Autowired
    private ILoginInfoService loginInfoService;
    @Autowired
    private IOrderTimeService orderTimeService;
    @Autowired
    private IUserInfoService userInfoService;

    @Override
    public int save(VideoAuth videoAuth) {
        return videoAuthMapper.insert(videoAuth);
    }

    @Override
    public int update(VideoAuth videoAuth) {
        return videoAuthMapper.updateByPrimaryKey(videoAuth);
    }

    @Override
    public VideoAuth get(Long id) {
        return videoAuthMapper.selectByPrimaryKey(id);
    }

    @Override
    public void audit(Long videoAuthId, Integer state, String remark) {
        //获取审核信息
        VideoAuth videoAuth = this.get(videoAuthId);
        //如果有审核对象,并且状态为未审核
        if (videoAuth != null && videoAuth.getState() == VideoAuth.STATE_NORMAL) {
            videoAuth.setState(state);
            videoAuth.setAuditTime(new Date());
            videoAuth.setRemark(remark);
            UserInfo applierUserInfo = userInfoService.select(videoAuth.getApplier().getId()); //获取申请审核人信息
            //如果审核通过,添加状态码为审核成功
            if (state == VideoAuth.STATE_PASS) {
                applierUserInfo.addState(BitStateUtil.HAS_VIDEOAUTH);
            } else {
                //设置申请审核人ID为null
                applierUserInfo.setVideoAuthId(null);
            }
            userInfoService.update(applierUserInfo);
            this.update(videoAuth);
        }
    }

    @Override
    public void videoAuthApply(Long timeid, String orderDate, Long auditorId) {
        LoginInfo auditorInfo = loginInfoService.get(auditorId);
        //判断审核人是否为空,是否是审核人
        if (auditorInfo == null || auditorInfo.getIsAuditor() == LoginInfo.ISAUDITOR_NO) {
            throw new DisplayableException("非法参数");
        }
        //判断预约时间是否有传递
        OrderTime orderTime = orderTimeService.get(timeid); //预约时间
        if (orderTime == null) {
            throw new DisplayableException("非法参数");
        }
        VideoAuth videoAuth = new VideoAuth();
        videoAuth.setApplier(UserContext.getCurrent());
        videoAuth.setApplyTime(new Date());
        videoAuth.setAuditor(auditorInfo);
        videoAuth.setState(VideoAuth.STATE_NORMAL);
        String beginDateStr = orderDate + " " + orderTime.getBegin();
        String endDateStr = orderDate + " " + orderTime.getEnd();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        try {
            videoAuth.setOrderBeginDate(sdf.parse(beginDateStr));
            videoAuth.setOrderEndDate(sdf.parse(endDateStr));
        } catch (Exception e) {
            throw new DisplayableException("非法参数");
        }
        this.save(videoAuth);
        UserInfo current = userInfoService.getCurrent();
        //设置审核Id
        current.setVideoAuthId(videoAuth.getId());
        userInfoService.update(current);
    }

    @Override
    public PageInfo queryPage(QueryObject qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        List result = videoAuthMapper.queryData(qo);
        return new PageInfo(result);
    }
}
